package com.kamistoat.meimeistore.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.meimeistore.product.entity.AttrEntity;
import com.kamistoat.meimeistore.product.service.AttrAttrgroupRelationService;
import com.kamistoat.meimeistore.product.service.AttrService;
import com.kamistoat.meimeistore.product.service.CategoryService;
import com.kamistoat.meimeistore.product.vo.AttrGroupRelationVo;
import com.kamistoat.meimeistore.product.vo.AttrGroupWithAttrVo;
import com.kamistoat.meimeistore.product.entity.AttrGroupEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeistore.product.service.AttrGroupService;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;


/**
 * 属性分组
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 15:16:42
 */
@RestController
@RequestMapping("meimeistoreproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 查询当前分组下的所有基础属性/销售属性
     *
     * @param attrgroupId 属性分组id
     * @return R
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrAttrGroupRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        // 因为此处前端需要的数据并没有特殊字段，所以直接用Entity是没问题的，没必要用Vo类
        List<AttrEntity> attrEntityList = attrService.getAttrRelation(attrgroupId);
        return R.ok().put("data", attrEntityList);
    }

    /**
     * 删除当前属性分组下的关联关系，只删关系，不是删除基础属性
     *
     * @param relationVos 列表，元素都是AttrGroupRelationVo
     * @return R
     */
    @RequestMapping("/attr/relation/delete")
    public R attrAttrGroupRelationDelete(@RequestBody List<AttrGroupRelationVo> relationVos) {
        attrService.deleteRelation(relationVos);
        return R.ok();
    }

    /**
     * 获取当前属性分组所在的三级菜单下所有未绑定属性分组的基础属性
     *
     * @param attrgroupId 要查的属性分组id
     * @param params      分页查询参数
     * @return R
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrService.getNoRelation(attrgroupId, params);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 为属性分组增加与基础属性的关联
     *
     * @param relationVos
     * @return R
     */
    @RequestMapping("/attr/relation")
    public R attrAddRelation(@RequestBody List<AttrGroupRelationVo> relationVos) {
        attrAttrgroupRelationService.addRelaition(relationVos);
        return R.ok();
    }

    /**
     * 返回三级分类下的属性分组列表并嵌套属性列表
     *
     * @param catelogId 三级分类ID
     * @return R
     */
    @RequestMapping("/{catelogId}/withattr")
    public R attrGroupWithAttr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrVo> attrGroupWithAttrVoList = attrGroupService.getAttrGroupWithAttr(catelogId);
        return R.ok().put("data", attrGroupWithAttrVoList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meimeistoreproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 只查询对应菜单的属性列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("meimeistoreproduct:attrgroup:list")
    public R listSingle(@PathVariable("categoryId") long categoryId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params, categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("meimeistoreproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 首先获取出该属性分组所属的菜单Id
        Long catelogId = attrGroup.getCatelogId();
        // 在CategoryService中定义一个寻找全部父菜单Id的方法，直接接收上面获取到的catelogId，返回一个long[]
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        // 返回前端的时候带着这个变量
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meimeistoreproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meimeistoreproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meimeistoreproduct:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        // TODO 还得删除一系列属性的关联关系
        return R.ok();
    }

}
